import 'package:after_sale_plugin/routes.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import '../../constant.dart';
import '../../package_event.dart';
import '../models/order_model.dart';
import 'state.dart';

class OrderHistoryListController extends LxBaseScrollOpacityController {
  // 状态
  OrderHistoryState state = OrderHistoryState();

  @override
  onInit() async {
    super.onInit();

    getOrderList();

    PackageEventStream().on.listen((event) {
      if (event == REFRESH_ORDER_LIST) {
        getOrderList(hidenLoading: true);
      }
    });
  }

  // 订单列表
  getOrderList({bool hidenLoading = false}) {
    PackageEvent.orderList((p0) {
      if (p0['data'] == null) return;
      List<dynamic> deviceMap = p0['data'];
      List<ReportOrderInfo> list = deviceMap
          .map((e) => ReportOrderInfo.fromMap(e as Map<String, dynamic>))
          .toList();
      state.orderInfoList.value = list;
    }, param: hidenLoading);
  }

  hisotyDetail(int index) {
    Get.toNamed(Routes.orderDetail, arguments: state.orderInfoList[index].id);
  }
}
